namespace Admin2025.Application.common;

public class ApiResult
{
    public int Code { get; set; } //0=成功，非0=业务错误
    public string Message { get; set; } = " ";
    public object? Data { get; set; }
    public static ApiResult Success(object? data = null, string? message = null)
    {
        return new ApiResult
        {
            Code = 200,
            Message = message ?? "操作成功",
            Data = data,

        };
    }

    public static ApiResult Fail(int code, string message)
    {
        return new ApiResult
        {
            Code = code,
            Message = message
        };
    }
    public static ApiResult SuccessPaged<T>(PagedResult<T> pagedResult)
    {
        return new ApiResult
        {
            Code = 200,
            Message = "操作成功",
            Data = new
            {
                totalCount = pagedResult.TotalCount,
                totalPage = pagedResult.TotalCount > 0 ? (int)Math.Ceiling((double)pagedResult.TotalCount / pagedResult.PageSize) : 0,
                pageIndex = pagedResult.PageIndex,
                pageSize = pagedResult.PageSize,
                items = pagedResult.Items
            }
        };
    }
}

public class ApiResult<T>
{
    public int Code { get; set; }
    public string Message { get; set; } = "";
    public T? Data { get; set; }
    public bool Succeeded => Code == 1000;
    public static ApiResult<T> Success(T? data = default, string? message = null) => new ApiResult<T> { Code = 1000, Message = message ?? "操作成功", Data = data };
    public static ApiResult<T> Failed(string message, int code = 400) => new ApiResult<T> { Code = code, Message = message };
    public static ApiResult<T> Failed(int code,string message) => new ApiResult<T> { Code = code, Message = message };
}